﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using ZhangZhao.Common;

namespace ZhangZhao.WebAPI
{
    /// <summary>
    /// Summary description for UploadImage
    /// </summary>
    public class UploadPhoto : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //获取上传的数据流
            Stream sr = context.Request.InputStream;
            try
            {
                string filename = DateTime.Now.Ticks.ToString();
                byte[] buffer = new byte[4096];
                int bytesRead = 0;
                string UserName = context.Request["UserName"];
                string ShortDescription = context.Request["ShortDescription"];

                if (!string.IsNullOrWhiteSpace(UserName))
                {
                    string CreatePath = context.Server.MapPath(string.Format("~/Upload/{0}", UserName));

                    if (!Directory.Exists(CreatePath))
                    {
                        Directory.CreateDirectory(CreatePath);
                    }

                    string LargeImagePath = CreatePath + "/" + filename + ".jpg";
                    string ThumbImagePath80 = CreatePath + "/" + filename + "_320.jpg";

                    //将当前数据流写入服务器端文件夹Upload/UserName下
                    using (FileStream fs = File.Create(LargeImagePath, 4096))
                    {
                        while ((bytesRead = sr.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            //向文件中写信息
                            fs.Write(buffer, 0, bytesRead);
                        }
                    }

                    //生成缩略图
                    ImageHelper.MakeThumbnail(LargeImagePath, ThumbImagePath80, 320, 320, "Cut");

                    #region 保存到数据库
                    ZhangZhao.BLL.H_UserGatherThirdPhoto bll = new BLL.H_UserGatherThirdPhoto();
                    bll.Add(new ZhangZhao.Model.H_UserGatherThirdPhoto()
                    {
                        CreatedTime = DateTime.Now,
                        IsLike = "1",
                        ShortDescription = ShortDescription,
                        ThirdPhotoLarge = filename + ".jpg",
                        ThirdPhotoThumb = filename + "_320.jpg",
                        ThirdUserName = UserName,
                        UserName = UserName,
                        ViewCount = 1
                    });
                    #endregion

                    context.Response.ContentType = "text/plain";
                    context.Response.Write(Output.Success + ":Upload/" + UserName + "/" + filename + ".jpg");
                }
            }
            catch (Exception e)
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write(Output.Fail + ":" + e.Message);
            }
            finally
            {
                sr.Dispose();
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}